using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;
using System.Web.Security;
using System.Data.Linq.SqlClient;

namespace VoiceAd.DAL
{
    public class StationRepository : IStationRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public StationRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblStation> GetListStations()
        {
            return context.tblStations.ToList();
        }

        public IEnumerable<tblStation> GetListSearchStations(string StationName)
        {
            IEnumerable<tblStation> lst = from a in context.tblStations
                                          where SqlMethods.Like(a.StationName, "%" + StationName + "%")
                                          select a;
            return lst;
        }

        public tblStation GetStationById(int id)
        {
            return context.tblStations.Where(x => x.Id == id).FirstOrDefault();
        }

        public IEnumerable<tblStation> GetListStationByProvinceId(int lProvinceID)
        {
            return context.GetListStationByProvinceId(lProvinceID);
        }

        public IEnumerable<tblBroadCast> tblStationGetBroadCasttime(int campId, int StationId)
        {
            return context.tblStationGetBroadCasttime(campId, StationId);
        }

        public tblStation GetStationByDistrictId(int id)
        {
            return context.tblStations.Where(x => x.DistrictId == id).FirstOrDefault();
        }

        public tblStation GetStationByUser()
        {
            string strUser = Membership.GetUser().UserName;
            return context.tblStations.Where(x => x.UserName == strUser).FirstOrDefault();
        }

        public void InsStation(tblStation tblstation)
        {
            context.tblStations.InsertOnSubmit(tblstation);
            context.SubmitChanges();
        }

        public void UpdStation(tblStation tblstation)
        {
            tblStation obj = context.tblStations.Where(x => x.Id == tblstation.Id).First();
            obj.UserName = tblstation.UserName;
            obj.StationName = tblstation.StationName;
            obj.DistrictId = tblstation.DistrictId;
            obj.Address = tblstation.Address;
            obj.DanSo = tblstation.DanSo;
            obj.CumLoa = tblstation.CumLoa;
            obj.Email = tblstation.Email;
            obj.UnitPrice = tblstation.UnitPrice;
            obj.SoLan = tblstation.SoLan;
            obj.SoXa = tblstation.SoXa;
            obj.NgayPhat = tblstation.NgayPhat;
            obj.GhiChu = tblstation.GhiChu;
            context.SubmitChanges();
        }

        public void DelStation(int id)
        {
            tblStation obj = context.tblStations.Single(x => x.Id == id);
            context.tblStations.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
